package javaweb.userlogin;

import bean.UserLogin;
import dao.UserInfoDao;
import dao.impl.UserInfoDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("get--------------------");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("utf-8");

        UserInfoDao dao = new UserInfoDaoImpl();
        UserLogin user = new UserLogin();
        List<UserLogin> userLogins = dao.allInfo();

        String userName = req.getParameter("userName");
        String passWord = req.getParameter("passWord");

        user.setUserName(userName);
        user.setPassWord(passWord);

        if (userLogins.contains(user)){
            System.out.println("用户登录成功~");
            //req.getRequestDispatcher("/success.html").forward(req,resp);
            resp.sendRedirect("/userlogin/success.html");
        }else{
            System.out.println("用户登录失败！");
            //req.getRequestDispatcher("/false.html").forward(req,resp);
            resp.sendRedirect("/userlogin/false.html");
        }
    }
}
